// Cubic Application, Load ExplorerPerspective
package org.pentaho.cubic.client;

import java.util.HashMap;

//import org.pentaho.cubic.client.commands.model.ICommandExec;
import org.pentaho.cubic.client.dialogs.MessageDialogBox;
import org.pentaho.cubic.client.dialogs.WaitDialog;
import org.pentaho.cubic.client.explorer.ExplorerPerspective;
import org.pentaho.cubic.client.explorer.PluginOptionsHelper;
import org.pentaho.cubic.client.explorer.settings.CubicSettingsManager;
import org.pentaho.cubic.client.explorer.settings.ICubicSettingsListener;
import org.pentaho.cubic.client.menubars.CubicMenuBar;
import org.pentaho.cubic.client.panels.GlassPane;
import org.pentaho.cubic.client.panels.GlassPaneNativeListener;
import org.pentaho.cubic.client.toolbars.CubicToolBar;
import org.pentaho.cubic.client.toolbars.MainToolBar;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CubicApplication implements ICubicSettingsListener {
	private static final int defaultTopPanelHeight = 75; //$NON-NLS-1$
	
	public static boolean showAdvancedFeatures = true;
	//private ICommandExec commandExec = GWT.create(ICommandExec.class);
	
	DockLayoutPanel applicationPanel;
	VerticalPanel applicationTopPanel;
	
	CubicMenuBar applicationMenuBar;
	MainToolBar applicationToolBar;
	
	ExplorerPerspective explorerPerspective;

	public void loadApplication() {
		applicationPanel = new DockLayoutPanel(Unit.PX);
		applicationTopPanel = new VerticalPanel();
		
		applicationMenuBar = new CubicMenuBar();	// Next build menubar for unique user (admin/etc.) [checkout buildMenuBar function]
		applicationToolBar = new MainToolBar();	// Next build toobar for unique user (admin/etc.)
		//applicationToolBar = new CubicToolBar();	// Next build toobar for unique user (admin/etc.)
		
		explorerPerspective = ExplorerPerspective.getInstance();
		
		applicationTopPanel.setWidth("100%");		
		applicationTopPanel.add(applicationMenuBar);
		applicationTopPanel.add(applicationToolBar);		
		
		applicationPanel.addNorth(applicationTopPanel, defaultTopPanelHeight);		// Menu & ToolBar Height
		applicationPanel.add(explorerPerspective);
		
		RootPanel.get().clear();
		RootLayoutPanel.get().add(applicationPanel);
		RootPanel.get().add(WaitDialog.getInstance());
		
		// registered our native JSNI hooks
		setupNativeHooks(this);

		// listen to any reloads of mantle settings
		CubicSettingsManager.getInstance().addMantleSettingsListener(this);
	}

	public native void setupNativeHooks(CubicApplication cubic)
	/*-{
	    $wnd.cubic_initialized = true;

	    $wnd.cubic_showMessage = function(title, message) {
      		cubic.@org.pentaho.cubic.client.CubicApplication::showMessage(Ljava/lang/String;Ljava/lang/String;)(title, message);
    	}
	}-*/;
	/*
	@SuppressWarnings("unused")
	private void executeCommand(String commandName) {
		commandExec.execute(commandName);
	}
	*/

	private void addGlassPaneListener(JavaScriptObject obj) {
		GlassPane.getInstance().addGlassPaneListener(new GlassPaneNativeListener(obj));
	}

	private void showMessage(String title, String message) {
		MessageDialogBox dialog = new MessageDialogBox(title, message, true, false, true);
		dialog.center();
	}

	public void onFetchCubicSettings(HashMap<String, String> settings) {
		
		// update supported file types
		PluginOptionsHelper.buildEnabledOptionsList(settings);

		boolean isAdministrator = "true".equals(settings.get("is-administrator"));
		explorerPerspective.setAdministrator(isAdministrator);

	}

}
